<?php

namespace admin\models;

use Yii;

/**
 * This is the model class for table "mm_user_detail".
 *
 * @property integer $md_id
 * @property integer $mu_id
 * @property string $md_work
 * @property string $md_mon_salary
 * @property string $md_year_salary
 * @property string $md_hascar
 * @property string $md_cartype
 * @property string $md_isowner
 * @property string $md_location
 * @property string $md_house_area
 * @property string $md_loan
 * @property string $md_house_address
 * @property string $md_marry
 * @property string $md_despid
 * @property string $md_child
 * @property string $md_child_belong
 * @property string $md_selfdiscr
 *
 * @property Info $mu
 */
class UserDetail extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mm_user_detail';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['md_work','md_mon_salary','md_year_salary','md_marry'],'required','on'=>['add','update']],
            [['mu_id'], 'integer'],
            [['md_mon_salary', 'md_year_salary'], 'number'],
            [['md_hascar', 'md_isowner', 'md_loan', 'md_marry', 'md_child', 'md_child_belong','md_despid','md_house_address'], 'string'],
            [['md_work'], 'string', 'max' => 255],
            [['md_cartype'], 'string', 'max' => 64],
            [['md_location'], 'string', 'max' => 128],
            [['md_house_area'], 'string', 'max' => 100],
            [['md_selfdiscr'], 'string', 'max' => 100],
            [['md_marry'],'checkMarry','on'=>['add','update']],
            [['md_hascar'],'checkCar','on'=>['add','update']],
            [['md_isowner'],'checkOwner','on'=>['add','update']],
            [['md_child'],'checkChild','on'=>['add','update']],
            [['mu_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserInfo::className(), 'targetAttribute' => ['mu_id' => 'mi_id']],
        ];
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['add']=['md_work','md_mon_salary','md_year_salary','md_hascar','md_cartype','md_isowner','md_location',
            'md_house_area','md_loan','md_marry','md_despid', 'md_child_belong','md_child','md_selfdiscr','md_house_address'];
        $scenarios['update']=['md_work','md_mon_salary','md_year_salary','md_hascar','md_cartype','md_isowner','md_location',
            'md_house_area','md_loan','md_marry','md_despid', 'md_child_belong','md_child','md_selfdiscr','md_house_address'];
        return $scenarios;
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'md_id' => '详细信息表id',
            'mu_id' => '用户id',
            'md_work' => '工作单位',
            'md_mon_salary' => '月薪',
            'md_year_salary' => '年薪',
            'md_hascar' => '是否有车',
            'md_cartype' => '车型',
            'md_isowner' => '是否有房',
            'md_location' => '房屋地址',
            'md_house_area' => '房屋面积',
            'md_house_address'=>'房屋地址',
            'md_loan' => '有无贷款',
            'md_marry' => '婚姻状态',
            'md_despid' => '离婚证号',
            'md_child' => '有无小孩',
            'md_child_belong' => '小孩归属',
            'md_selfdiscr' => '自我描述',
        ];
    }

    public function checkMarry(){
        if($this->md_marry=='离异'){
            if(empty($this->md_despid)){
                $this->addError('md_despid','请填写离婚证号');
            }
            if(empty($this->md_child)){
                $this->addError('md_child','请选择是否有小孩');
            }
        }elseif($this->md_marry=='丧偶'){
            if(empty($this->md_child)){
                $this->addError('md_child','请选择是否有小孩');
            }
        }
    }
    public function checkCar(){
        if($this->md_hascar=='有'){
            if(empty($this->md_cartype)){
                $this->addError('md_cartype','请填写车型');
            }
        }
    }
    public function checkOwner(){
        if($this->md_isowner=='有'){
            if(empty($this->md_house_area)){
                $this->addError('md_house_area','请填写房屋面积');
            }
            if(empty($this->md_house_address)){
                $this->addError('md_house_address','请填写房屋地址');
            }
        }
    }
    public function checkChild(){
        if($this->md_child=='有'){
            if(empty($this->md_child_belong)){
                $this->addError('md_child_belong','请选择小孩归属');
            }
        }
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMu()
    {
        return $this->hasOne(UserInfo::className(), ['mi_id' => 'mu_id']);
    }
}
